package com.soda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.soda.bean.Fmenu;
import com.soda.bean.Menu;
import com.soda.service.FmenuService;
import com.soda.service.MenuService;
import com.soda.service.impl.FmenuServiceImpl;
import com.soda.service.impl.MenuServiceImpl;
import com.soda.util.MenuInfo;
import com.soda.util.PageInfo;
import com.soda.util.SelectRoleInfo;

public class MenuServlet extends HttpServlet {

	MenuService ms = new MenuServiceImpl();
	FmenuService fs = new FmenuServiceImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");

		String param = request.getParameter("param");
		if (param != null) {
			// 全查菜单
			if ("showAllMenu".equals(param)) {
				if (request.getParameter("aaa") != null) {
					request.getSession().removeAttribute("smi");
				}
				showAllMenu(request, response);
			}
			if ("showMenuBuId".equals(param)) {
				showMenuBuId(request, response);
			}
			if ("editMenuBuId".equals(param)) {
				editMenuBuId(request, response);
			}
			if ("saveMenuInfo".equals(param)) {
				saveMenuInfo(request, response);
			}
			if ("editState".equals(param)) {
				editState(request, response);
			}
			if ("deleteMenuBuId".equals(param)) {
				deleteMenuBuId(request, response);
			}
			if ("addMenu".equals(param)) {

				addMenu(request, response);
			}
			if ("addMenuInfo".equals(param)) {
				addMenuInfo(request, response);
			}
			if ("ajaxmenuname".equals(param)) {
				ajaxmenuname(request, response);
			}
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void showAllMenu(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		PageInfo pi = new PageInfo();
		SelectRoleInfo smi = new SelectRoleInfo();// 由于模糊查询的条件和角色相同。故可用同一个工具类

		// 接收参数
		String name = request.getParameter("name");
		String state = request.getParameter("state");

		if (name != null) {
			smi.setName(name.length() == 0 ? null : name);
			smi.setState(state.length() == 0 ? null : Integer.parseInt(state));
			request.getSession().setAttribute("smi", smi);
		} else {
			smi = (SelectRoleInfo) request.getSession().getAttribute("smi");
		}

		pi.setPer(10);
		pi.setTotal(ms.getMenuCountByInfo(smi));

		String page = request.getParameter("page");
		if (page != null) {
			pi.setPage(Integer.parseInt(page));
		}

		// 根据模糊查询查符合条件的数据
		List<MenuInfo> menus = ms.getAllMenu(smi, pi);

		request.setAttribute("pi", pi);
		request.setAttribute("menus", menus);
		request.getRequestDispatcher("/houtai/pages/menu/list.jsp").forward(
				request, response);
	}

	public void showMenuBuId(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		int id = Integer.parseInt(request.getParameter("id"));
		String url = request.getParameter("url");
		MenuInfo menu;
		if (url.length() != 0) {
			
			menu = ms.getMenuById(id);

		} else {
		
			menu = fs.getFmenuById(id);

		}
		
		
	
		// 查询所有父级菜单
		List<Fmenu> fmenus = fs.getAllFmenu();
		// 将查询到的数据保存
		request.setAttribute("menu", menu);
		request.setAttribute("fmenus", fmenus);
		request.getRequestDispatcher("/houtai/pages/menu/detail.jsp").forward(
				request, response);
	}

	// 修改菜单信息
	private void editMenuBuId(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		int id = Integer.parseInt(request.getParameter("id"));
		String url = request.getParameter("url");
		// Menu menu=ms.getMenuById(id);
		MenuInfo menu;
		if (url.length() != 0) {
			menu = ms.getMenuById(id);

		} else {
			menu = fs.getFmenuById(id);

		}
		// 查询所有父级菜单
		List<Fmenu> fmenus = fs.getAllFmenu();

		// 将查询到的数据保存
		request.setAttribute("menu", menu);
		request.setAttribute("fmenus", fmenus);
		request.getRequestDispatcher("/houtai/pages/menu/edit.jsp").forward(
				request, response);
	}

	private void saveMenuInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		Menu menu = new Menu();
		Fmenu fmenu = new Fmenu();

		int id = Integer.parseInt(request.getParameter("id"));
		String menname = request.getParameter("menname");
		String menurl = request.getParameter("menurl");

		String menremark = request.getParameter("menremark");

		// 应判断fmenid是否为-1，如果为-1，则说明添加的是父菜单
		// url为空表示为父菜单
		if (menurl.length() == 0) {

			// 应先删除对应的子菜单，然后再添加子菜单
			// 添加父菜单
			fmenu.setFmeId(id);
			fmenu.setFmeName(menname);
			fmenu.setFmeRemark(menremark);
			fmenu.setFmeState(1);

			fs.updateMenuInfo(fmenu);

		} else {
			// 添加子菜单
			int fmenid = Integer.parseInt(request.getParameter("fmenid"));
			int menstate = Integer.parseInt(request.getParameter("menstate"));
			menu.setMenId(id);
			menu.setMenName(menname);
			menu.setMenUrl(fmenid == -1 ? "" : menurl);
			menu.getFmenu().setFmeId(fmenid);
			menu.setMenState(menstate);
			menu.setMenRemark(menremark);
			ms.updateMenuInfo(menu);
		}
		showAllMenu(request, response);
	}

	public void editState(HttpServletRequest request,
			HttpServletResponse response) {

		int id = Integer.parseInt(request.getParameter("id"));
		int state = Integer.parseInt(request.getParameter("state"));
		ms.editState(id, state);
		// 更改状态成功
	}

	public void deleteMenuBuId(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String url = request.getParameter("url");
		// 删除
		if (url.length() != 0) {
			ms.deleteMenuById(id);
		} else {
			fs.deleteMenuId(id);
		}
		showAllMenu(request, response);
	}

	public void addMenu(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 查询所有父级菜单
		List<Fmenu> fmenus = fs.getAllFmenu();
		request.setAttribute("fmenus", fmenus);
		request.getRequestDispatcher("/houtai/pages/menu/add.jsp").forward(
				request, response);

	}

	public void addMenuInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		Menu menu = new Menu();
		Fmenu fmenu = new Fmenu();

		String menname = request.getParameter("menname");
		String menurl = request.getParameter("menurl");
		int fmenid = Integer.parseInt(request.getParameter("fmenid"));
		int menstate = Integer.parseInt(request.getParameter("menstate"));
		String menremark = request.getParameter("menremark");

		// 应判断fmenid是否为-1，如果为-1，则说明添加的是父菜单
		if (fmenid == -1) {
			fmenu.setFmeName(menname);
			fmenu.setFmeRemark(menremark);
			fmenu.setFmeState(menstate);

			// 添加父菜单
			//
			fs.addFmenuInfo(fmenu);

		} else {
			// 添加子菜单
			menu.setMenName(menname);
			menu.setMenUrl(fmenid == -1 ? "" : menurl);
			menu.getFmenu().setFmeId(fmenid);
			menu.setMenState(menstate);
			menu.setMenRemark(menremark);
			ms.addMenuInfo(menu);

		}
		showAllMenu(request, response);

	}

	public void ajaxmenuname(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String menname = new String(request.getParameter("menname").getBytes(
				"ISO-8859-1"), "utf-8");
		String ids = request.getParameter("id");
		if (ids != null) {
			int id = Integer.parseInt(ids);
			String mname = ms.getMenuNameById(id);
			if (mname.equals(menname)) {
				return;
			} else {
				if (ms.ajaxMenName(menname) > 0) {
					out.print("菜单名已存在");

				} else {

					out.print("√");
				}
			}
		}else{
			if (ms.ajaxMenName(menname) > 0) {
				out.print("菜单名已存在");

			} else {

				out.print("√");
			}
		}

	}
}
